<?php

class HmOfferController extends Fw_Controller_Action
{

	public function init()
	{
		$this->setDisplay('backoffice');
		$this->initSession();
	}

	public function indexAction()
	{
		$model = $this->_getModel('HmOffer');
		$view_list = $this->_model;
		$rowset = $view_list->fetchAll();

		$rowCount = count($rowset);
		$this->view->view_list = $rowset;
		$this->view->rowCount  = $rowCount;
		
		require_once APPLICATION_PATH . '/models/HmStatus.php';
		$statusTable = new Model_HmStatus();
		$status_list = $statusTable->fetchAll();
		$this->view->status_list = $status_list;
		
		require_once APPLICATION_PATH . '/models/HmType.php';
		$typeTable = new Model_HmType();
		$type_list = $typeTable->fetchAll();
		$this->view->type_list = $type_list;
	}
	
	public function addAction()
	{
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
            if ($formData) {
            	
				$model = $this->_getModel('HmOffer');
				$add   = $this->_model;
				
				$row                = $add->createRow();
                $row->user_id       = $this->id;
                $row->date_register = date('Y-m-d H:i:s');
                $row->volume        = $this->_request->getPost('volume');
                $row->description   = $this->_request->getPost('description');
                $row->type_id       = $this->_request->getPost('type_id');
                $row->status_id     = $this->_request->getPost('status_id');
				$row->save();
                
                return $this->_helper->redirector('index');
            }
        }
		
		require_once APPLICATION_PATH . '/models/HmStatus.php';
		$statusTable = new Model_HmStatus();
		$status_list = $statusTable->fetchAll();
		$statusRowCount = count($status_list);
		
		$this->view->status_list    = $status_list;
		$this->view->statusRowCount = $statusRowCount;
		
		require_once APPLICATION_PATH . '/models/HmType.php';
		$typeTable = new Model_HmType();
		$type_list = $typeTable->fetchAll();
		$typeRowCount = count($type_list);
		
		$this->view->type_list    = $type_list;
		$this->view->typeRowCount = $typeRowCount;
	}
	
	public function editAction()
	{
		$model    = $this->_getModel('HmOffer');
		$edit     = $this->_model;
		$offer_id = (int)$this->_request->getParam('offer_id');
		
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
            if ($formData) {
				$offer_id = (int)$this->_request->getPost('offer_id');
                $row = $edit->fetchRow('offer_id='.$offer_id);

                $row->user_id     = $this->user_id;
                $row->volume      = $this->_request->getPost('volume');
                $row->description = $this->_request->getPost('description');
                $row->status_id   = $this->_request->getPost('status_id');
                $row->type_id     = $this->_request->getPost('type_id');

                $row->save();
                
                return $this->_helper->redirector('index');
            }
        }
		$this->view->edit = $edit->fetchRow('offer_id='.$offer_id);
		
		require_once APPLICATION_PATH . '/models/HmStatus.php';
		$statusTable = new Model_HmStatus();
		$status_list = $statusTable->fetchAll();
		$statusRowCount = count($status_list);
		
		$this->view->status_list    = $status_list;
		$this->view->statusRowCount = $statusRowCount;
		
		require_once APPLICATION_PATH . '/models/HmType.php';
		$typeTable = new Model_HmType();
		$type_list = $typeTable->fetchAll();
		$typeRowCount = count($type_list);
		
		$this->view->type_list    = $type_list;
		$this->view->typeRowCount = $typeRowCount;
	}
	
	public function showAction()
	{
		$offer_id = (int)$this->_request->getParam('offer_id');
				
		$model = $this->_getModel('HmOffer');
		$show = $this->_model;
		$this->view->show = $show->fetchRow('offer_id='.$offer_id);
	}
	
	public function deleteAction()
	{
		if ($this->_request->isPost()) {
            $offer_id = (int)$this->_request->getPost('offer_id');
            $del = $this->_request->getPost('del');
            if ($del == 'Yes' && $offer_id > 0) {
                
				$model = $this->_getModel('HmOffer');
				$remove = $this->_model;
				
                $where = 'offer_id = ' . $offer_id;
                $remove->delete($where);
            }
            return $this->_helper->redirector('index');
        } else {
            $offer_id = (int)$this->_request->getParam('offer_id');
            if ($offer_id > 0) {
                
				$model = $this->_getModel('HmOffer');
				$remove = $this->_model;
				
                $this->view->remove = $remove->fetchRow('offer_id='.$offer_id);
            }
        }
	}
	/*
    protected function _getModel()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . 'models/HmOffer.php';
            $this->_model = new Model_HmOffer();
        }
        return $this->_model;
    }
	*/
}